package win.acfelis.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import win.acfelis.dao.AdverMapper;
import win.acfelis.domain.DetailModel;
import win.acfelis.service.AdverService;
import win.acfelis.tools.ApiResult;
import win.acfelis.tools.AppContent;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: acfelis
 * @Date: 17-11-18
 */
@Transactional
@Service
public class AdverServiceImpl implements AdverService {
    @Resource
    AdverMapper adverMapper;


    @Override
    public Map<String, List<Map<String, Object>>> getInfo() {
        try {
            Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
            List<Map<String, Object>> top = adverMapper.getInfo("1");
            List<Map<String, Object>> ordinary = adverMapper.getInfo("0");
            Map<String, Object> time = new HashMap<String, Object>();

            List<Map<String, Object>> yesterday = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> tommorrow = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> acquired = new ArrayList<Map<String, Object>>();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar rightNow = Calendar.getInstance();
            Date now = sdf.parse(sdf.format(new Date()));
            //初始化时间
            rightNow.setTime(now);
            rightNow.add(Calendar.DAY_OF_YEAR, 1);
            Date tomm = rightNow.getTime();//明天
            time.put("tomm", tomm);
            rightNow.setTime(now);
            rightNow.add(Calendar.DAY_OF_YEAR, 2);
            Date acqu = rightNow.getTime();//后天
            time.put("acqu", acqu);
            rightNow.setTime(now);
            rightNow.add(Calendar.DAY_OF_YEAR, -1);
            Date yest = rightNow.getTime();//昨天
            time.put("yest", yest);
            //获取昨天的数据
            for (int i = 0; i < ordinary.size(); i++) {
                String opendate = ordinary.get(i).get("opendate").toString();
                Date openDateTime = sdf.parse(opendate);
                if (tomm.getTime() == openDateTime.getTime()) {
                    tommorrow.add(ordinary.get(i));
                } else if (acqu.getTime() == openDateTime.getTime()) {
                    acquired.add(ordinary.get(i));
                } else if (yest.getTime() == openDateTime.getTime()) {
                    yesterday.add(ordinary.get(i));
                }
            }
            map.put("top", top);
            map.put("tommorrow", tommorrow);
            map.put("yesterday", yesterday);
            map.put("acquired", acquired);
            List<Map<String, Object>> timeList = new ArrayList<Map<String, Object>>();
            timeList.add(time);
            map.put("time", timeList);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map<String, List<Map<String, Object>>> list(String keyword) {
        Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
        result.put("list", adverMapper.list(keyword));
        return result;
    }

    @Override
    public Map<String, Object> detail(String guid) {
        return adverMapper.detail(guid);
    }

    @Override
    public ApiResult add(DetailModel info) {
        try{
            int result = 0;
            //判断新增还是修改
            if (AppContent.DETAIL_TYPE_ADD.equals(info.getType())) {
                //新增
                info.setGuid(AppContent.createGuid());
                result = adverMapper.addAdverInfo(info);
            } else if (AppContent.DETAIL_TYPE_UPDATE.equals(info.getType())) {
                //修改
                result = adverMapper.updateAdverInfo(info);
            }else{
                return new ApiResult().failure(AppContent.ACTION_FAILD,"操作失败");
            }
            if(result == 1){
                return new ApiResult().success(AppContent.ACTION_SUCC,"操作成功");
            }else{
                return new ApiResult().failure(AppContent.ACTION_FAILD,"操作失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult().failure(AppContent.ACTION_ERROR,"操作异常");
        }
    }


}
